# -*- coding: utf-8 -*-
"""
检索方式对比实验配置文件
用于论文数据收集的各种检索方式效果对比
"""

import os
from typing import Dict, List, Any

# Ollama配置
OLLAMA_CONFIG = {
    "base_url": "http://localhost:11434",
    "embedding_model": "bge-m3:latest",  # 用于向量检索的embedding模型
    "rerank_model": "bge-rerank-v2-m3",  # 用于rerank的模型
    "timeout": 30,
    "max_retries": 3
}

# 检索方式配置
RETRIEVAL_METHODS = {
    "vector_search": {
        "name": "向量检索",
        "description": "基于语义向量相似度的检索",
        "expected_precision": "中高",
        "expected_speed": "中",
        "use_case": "语义问答、长文本匹配",
        "enabled": True
    },
    "keyword_search": {
        "name": "关键词检索",
        "description": "基于关键词匹配的检索",
        "expected_precision": "高",
        "expected_speed": "高",
        "use_case": "精确匹配、日志/代码",
        "enabled": True
    },
    "hybrid_search": {
        "name": "混合检索",
        "description": "结合向量和关键词的混合检索",
        "expected_precision": "高",
        "expected_speed": "中",
        "use_case": "综合场景",
        "enabled": True
    },
    "knowledge_graph": {
        "name": "知识图谱",
        "description": "基于知识图谱的推理检索",
        "expected_precision": "高",
        "expected_speed": "中低",
        "use_case": "事实推理、专业领域",
        "enabled": True
    },
    "rerank": {
        "name": "Rerank",
        "description": "多轮重排序检索",
        "expected_precision": "最高",
        "expected_speed": "低",
        "use_case": "高价值场景",
        "enabled": True
    },
    "multi_step_reasoning": {
        "name": "多步推理",
        "description": "基于多步推理的检索",
        "expected_precision": "高",
        "expected_speed": "低",
        "use_case": "复杂问题求解",
        "enabled": True
    },
    "structured_search": {
        "name": "结构化检索",
        "description": "基于结构化数据的检索",
        "expected_precision": "最高",
        "expected_speed": "高",
        "use_case": "数据库、API查询",
        "enabled": True
    }
}

# 评估指标配置（检索质量评估）
EVALUATION_METRICS = {
    "retrieval_precision": {
        "name": "检索精度",
        "description": "检索结果中相关文档的比例",
        "weight": 0.25
    },
    "retrieval_recall": {
        "name": "检索召回率",
        "description": "相关文档被检索到的比例",
        "weight": 0.25
    },
    "retrieval_f1": {
        "name": "检索F1分数",
        "description": "精度和召回率的调和平均",
        "weight": 0.20
    },
    "relevance_score": {
        "name": "相关性分数",
        "description": "检索结果与查询的相关程度",
        "weight": 0.15
    },
    "coverage_score": {
        "name": "覆盖度分数",
        "description": "检索结果的信息覆盖程度",
        "weight": 0.15
    }
}

# 评估配置
EVALUATION_CONFIG = {
    "metrics": [
        "retrieval_precision",
        "retrieval_recall", 
        "retrieval_f1",
        "relevance_score",
        "coverage_score"
    ],
    "top_k_values": [1, 3, 5, 10],
    "confidence_threshold": 0.5,
    "enable_detailed_analysis": True,
    "relevance_threshold": 0.6,  # 相关性判断阈值
    "coverage_threshold": 0.5,   # 覆盖度判断阈值
    "enable_cross_validation": True,
    "validation_split": 0.2
}

# 测试数据集配置
TEST_DATASET = {
    "categories": [
        "21100150",  # 大伙房水库
        "20800900",  # 柴河水库
        "21113800",  # 汤河水库
    ],
    "sample_size": 50,  # 每个类别的测试样本数
    "query_types": [
        "factual",      # 事实性问题
        "procedural",   # 程序性问题
        "analytical",   # 分析性问题
        "comparative"   # 比较性问题
    ]
}

# 检索方式配置（针对大伙房水库问题库优化）
RETRIEVAL_CONFIG = {
    # 向量检索配置
    "vector": {
        "embedding_method": "ollama",  # 使用Ollama的bge-m3模型
        "model_name": "bge-m3:latest",
        "similarity_threshold": 0.65,  # 提高阈值，确保精确匹配
        "max_tokens": 512,  # 增加token长度处理长文本
        "normalize_embeddings": True,
        "cache_path": "data/dahuofang_vector_cache.pkl",
        "top_k": 5,
        
        # 专业术语增强
        "domain_terms": [
            "大伙房水库", "浑河", "苏子河", "社河", "水利枢纽",
            "库容", "坝高", "发电量", "供水能力", "防洪标准",
            "生态补水", "水质监测", "环境保护", "水资源配置"
        ],
        
        # 数值信息处理
        "handle_numerical": True,
        "numerical_weight": 1.2,  # 数值匹配权重提升
        
        # 多语义层次
        "multi_level_embedding": True,
        "sentence_level_weight": 0.6,
        "document_level_weight": 0.4
    },
    
    # 关键词检索配置
    "keyword": {
        "match_threshold": 0.7,  # 提高匹配阈值
        "use_fuzzy_match": True,
        "fuzzy_threshold": 0.8,
        "cache_path": "data/dahuofang_keyword_cache.json",
        "top_k": 5,
        
        # 专业词典
        "domain_dictionary": {
            "水库": ["水库", "蓄水库", "人工湖", "水利工程"],
            "库容": ["库容", "蓄水量", "总库容", "有效库容"],
            "发电": ["发电", "水力发电", "电力生产", "发电量"],
            "供水": ["供水", "给水", "水源", "饮用水"],
            "防洪": ["防洪", "洪水控制", "防汛", "洪水调节"]
        },
        
        # 数值处理
        "numerical_matching": True,
        "unit_normalization": True,
        "range_query_support": True,
        
        # 权重配置
        "exact_match_weight": 2.0,
        "partial_match_weight": 1.0,
        "fuzzy_match_weight": 0.6,
        "numerical_match_weight": 1.5
    },
    
    # 混合检索配置
    "hybrid": {
        "vector_weight": 0.65,  # 向量检索权重
        "keyword_weight": 0.35,  # 关键词检索权重
        "fusion_method": "weighted_sum",
        "min_confidence": 0.4,
        "use_llm_rerank": True,
        "top_k": 5,
        
        # 动态权重调整
        "adaptive_weighting": True,
        "question_type_weights": {
            "numerical": {"vector": 0.4, "keyword": 0.6},  # 数值问题偏向关键词
            "conceptual": {"vector": 0.7, "keyword": 0.3},  # 概念问题偏向向量
            "factual": {"vector": 0.5, "keyword": 0.5}     # 事实问题平衡
        },
        
        # 结果融合优化
        "deduplication_threshold": 0.85,
        "result_diversification": True,
        "max_similar_results": 2
    },
    
    # 知识图谱检索配置
    "knowledge_graph": {
        "max_search_depth": 3,
        "min_confidence": 0.5,
        "use_reasoning": True,
        "cache_path": "data/dahuofang_kg_cache.json",
        "top_k": 5,
        
        # 实体类型定义
        "entity_types": {
            "水库": "水利设施",
            "河流": "自然水体",
            "功能": "作用类型",
            "参数": "技术指标",
            "地区": "地理位置",
            "时间": "时间节点"
        },
        
        # 关系类型定义
        "relation_types": {
            "位于": "地理关系",
            "具有": "属性关系",
            "用于": "功能关系",
            "影响": "因果关系",
            "包含": "包含关系",
            "连接": "连接关系"
        },
        
        # 领域知识
        "domain_knowledge": {
            "水库功能": ["防洪", "供水", "发电", "灌溉", "生态"],
            "技术参数": ["库容", "坝高", "装机容量", "年发电量"],
            "环境指标": ["水质", "生态流量", "环境容量"]
        }
    },
    
    # Rerank检索配置
    "rerank": {
        "initial_retrieval_size": 30,  # 增加初始检索数量
        "final_top_k": 5,
        "rerank_method": "ollama_rerank",
        "use_multiple_retrievers": True,
        "min_confidence": 0.6,
        "cache_path": "data/dahuofang_rerank_cache.json",
        
        # Ollama重排序配置
        "ollama_rerank_config": {
            "model_name": "bge-rerank-v2-m3",
            "max_length": 512,
            "batch_size": 8,
            "temperature": 0.1
        },
        
        # 特征权重（针对水库问题优化）
        "feature_weights": {
            "semantic_similarity": 0.25,    # 语义相似度
            "keyword_match": 0.25,         # 关键词匹配
            "numerical_accuracy": 0.20,    # 数值准确性（新增）
            "domain_relevance": 0.15,      # 领域相关性（新增）
            "answer_completeness": 0.10,   # 答案完整性
            "length_penalty": 0.05         # 长度惩罚
        },
        
        # 领域特定特征
        "domain_features": {
            "technical_terms_match": True,   # 技术术语匹配
            "numerical_precision": True,    # 数值精度
            "unit_consistency": True,       # 单位一致性
            "temporal_relevance": True      # 时间相关性
        }
    },
    
    # 多步推理检索配置
    "multi_step_reasoning": {
        "max_reasoning_steps": 4,  # 适中的推理步数
        "min_confidence_threshold": 0.65,
        "decomposition_method": "hybrid",  # 混合分解方法
        "search_strategy": "breadth_first",
        "enable_self_reflection": True,
        "cache_path": "data/dahuofang_multistep_cache.json",
        "top_k": 3,
        
        # 问题分解模板（水库专用）
        "decomposition_templates": {
            "功能询问": [
                "水库的主要功能是什么？",
                "这些功能如何实现？",
                "功能的具体效果如何？"
            ],
            "参数查询": [
                "相关的技术参数有哪些？",
                "这些参数的具体数值是多少？",
                "参数之间有什么关系？"
            ],
            "影响分析": [
                "产生了哪些影响？",
                "影响的范围和程度如何？",
                "如何评估这些影响？"
            ]
        },
        
        # 推理策略
        "reasoning_strategies": {
            "hierarchical": True,     # 层次化推理
            "causal": True,          # 因果推理
            "comparative": True,     # 比较推理
            "temporal": True         # 时序推理
        },
        
        # 质量控制
        "quality_control": {
            "min_evidence_count": 2,     # 最少证据数量
            "consistency_check": True,   # 一致性检查
            "completeness_check": True,  # 完整性检查
            "accuracy_validation": True  # 准确性验证
        }
    },
    
    # 结构化检索配置
    "structured_search": {
        "db_path": "data/dahuofang_structured.db",
        "enable_sql_generation": True,
        "enable_api_simulation": True,
        "max_results": 50,
        "cache_path": "data/dahuofang_structured_cache.json",
        "top_k": 5,
        
        # 数据模式定义
        "data_schema": {
            "questions": {
                "id": "INTEGER PRIMARY KEY",
                "question": "TEXT NOT NULL",
                "answer": "TEXT NOT NULL",
                "category": "TEXT",
                "subcategory": "TEXT",
                "difficulty": "TEXT",
                "keywords": "TEXT",
                "numerical_values": "TEXT",
                "created_time": "DATETIME",
                "updated_time": "DATETIME"
            }
        },
        
        # 查询模式识别
        "query_patterns": {
            "统计查询": r"(多少|数量|总数|计数|统计)",
            "分类查询": r"(分类|类型|种类|按.*分)",
            "排序查询": r"(排序|排名|最.*的|前.*名)",
            "范围查询": r"(范围|之间|从.*到|大于|小于)",
            "比较查询": r"(比较|对比|差异|相同|不同)",
            "聚合查询": r"(平均|总和|最大|最小|求和)"
        },
        
        # API端点模拟
        "api_endpoints": {
            "/search": "全文搜索",
            "/filter": "条件筛选",
            "/stats": "统计分析",
            "/categories": "分类信息",
            "/compare": "比较分析"
        }
    }
}

# 输出配置
OUTPUT_CONFIG = {
    "results_dir": "./results",
    "report_format": ["json", "csv", "markdown"],
    "include_charts": True,
    "chart_types": ["bar", "radar", "heatmap"]
}

# 日志配置
LOGGING_CONFIG = {
    "level": "INFO",
    "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
    "file": "./logs/retrieval_comparison.log"
}